package studentfunction;

import student.StudenList;
import student.Student;

import java.util.Scanner;

public class AddFunction implements IFunction{
    @Override
    public void work(StudenList studenList) {
        System.out.println("新增学生！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入学生姓名：");
        String name = scanner.nextLine();
        System.out.println("输入性别：");
        String sex = scanner.nextLine();
        System.out.println("输入班级：");
        String clas = scanner.nextLine();
        System.out.println("输入学号：");
        int schoolNuber = scanner.nextInt();
        Student student = new Student(name,sex,clas,schoolNuber);
        int currentSize = studenList.getUsedsize();//记录现在的学生总数量
        for (int i = 0; i < currentSize; i++) {
            Student tmp = studenList.getStudent(i);
            if(tmp.getName().equals(name)) {//遍历一遍数组，判断学生信息是否重复
                System.out.println("已经存在这名学生了，不能增加了");
                return;//退出work
            }
        }
        studenList.setStudent(student);//没有重复就增加学生
        //修改usedSize
        studenList.setUsedsize(currentSize+1);//因为在最后加了学生，所以数量加1
        System.out.println("添加学生信息成功");
    }
}
